var amqp = require('amqp');
var uuid = require('uuid');

var eventLog = "^_^";
var errorLog = "-_-||";

var mpInfo = {
    hosts : ["h3crd-wlan1", "h3crd-wlan2"],//微软云上mq服务主机
    connectOpt : {
        host : 'localhost',                //要连接的mq主机
        login : 'guest',                   //连接时的用户名
        password : '123456'
    },
    exchangeOpt : {                        //创建exchange的参数
        
    },
    queueOpt : {                           //创建queue的参数
        
    },
    queueName : "",                        //queue的名字
    sendOpt : {
        appId : null                       //发送消息时带的自己微服务的名字
    },
    connection : null,                     //微服务与mq连接的句柄
    exchange : null,                       //exchange的句柄
    msgRcvFunc : null,                     //使用者注册的用来接收消息的函数
};
var cbMap = new Map();

function setCb(callback) {
    var messageId = uuid.v1;
    cbMap.map(messageId, callback);
    return messageId;
}

function getCb(messageId) {
    return cbMap.getObject(messageId);
}

function send(routiongKey, msg, callback) {
    var messageId = setCb(callback);
    var exchange  = mqInfo.exchange;
    var sendOpt   = {
        appId   : mqInfo.sendOpt.appId,
        replyTo : mqInfo.queueName,
        type    : "send"
    };


    try {
        exchange.publish(soutionKey, sendOpt, msg);
    }
    catch (error) {
        console.log(errorLog + "send failed : %s", error);
    }
}

function reply(routionKey, messageId, msg) {
    var sendOpt = mqInfo.sendOpt;
    var exchange = mqInfo.exchange;
    
    sendOpt.type = "reply";
    sendOpt.messageid = messageId;
    try {
        exchange.publish(soutionKey, sendOpt, msg);
    }
    catch {
    }
}

function argProc() {
    var host = null;
    var login = null;
    var msgRcvFunc = null;
    var connectOpt = mqInfo.connectOpt;
    var sendOpt = mqInfo.sendOpt;

    if (arguments.length === 3) {
        host = arguments[0];
        login = arguments[1];
        msgRcvFunc = arguments[2];
    }
    else if (arguments.length === 2) {
        login = arguments[0];
        msgRcvFunc = arguments[1];
        for (var i in serviceNames) {
            if (login === serviceNames[i]) {
                host = mqInfo.hosts[i % mqInfo.hosts.length];
                break;
            }
        }
    }
    else {
        console.log("invalid arguments");
    }

    if (!host) {
        console.log("invalid serviceName %s", login);
        return ;
    }

    connectOpt.host   = host;
    connectOpt.login  = login;
    sendOpt.appId     = login;
    mqInfo.queueName  = login;
    mqInfo.msgRcvFunc = msgRcvFunc;
}

function mqinit() {
    var createOption = config.createOpt;
    var exchangeOpt  = config.exchangeOpt;
    var queueName    = config.queueName;
    var queueOpt     = config.queueOpt;
    var connection   = null;

    connection = amqp.createConnection(createOption);
    connection.on('ready', function() {
        connection.exchange('sendMsg-exchange', function (exchange) {
            console.log('Exchange ' + exchange.name + ' is open');
            mqInfo.exchange = exchange;
        });

        connection.queue(queueName, queueOpt, function (queue) {
            queue.bind();
            queue.subscribe(function (message, headers, deliveryInfo, messageObject) {
           });
        });
    });
    connection.on('error', function() {

    });
}

function message(message, headers, deliveryInfo, messageObject) {
    var type = deliveryInfo.type;
    var replyTo = deliveryInfo.replyTo;
    var messageId = deliveryInfo.messageId;

    if (type === "send") {
        mqInfo.msgRcvFunc(message, function(message) {
            reply(replyTo, messageId, message);
        });
    }
    else if (type === "reply") {
        cb = getCb(messageId);
        cb(message);
    }
}

module.exports = {
    
}
